home *** CD-ROM | disk | FTP | other *** search
/ QuickTime for the Web (2nd Edition) / QuickTime for the Web (2nd Edition).iso / pc / Demos / Mac / Matthew's Behaviors / Linesƒ / Lines1.0 < prev    next >
Encoding:
INI File  |  2001-09-10  |  8.7 KB  |  168 lines

  1. [Name]
  2. Lines v1.0 - Provides events for drawing simple lines
  3. By Matthew Peterson, matthew@pinoko.berkeley.edu
  4.  
  5. [Description]
  6. 2-19-2000
  7. This behavior consists of 2 functions. To use these functions, 
  8. place this behavior on any sprite. Set the input Global Variables, and
  9. Execute the functions.
  10. Note: Each line is a sprite. You must provide the ID of the sprite you would
  11. like to turn into a line. You may use MakeNewSprite( ) if you want
  12. to produce new sprites during runtime of your wired movie.
  13.  
  14. DrawLine     :        This event uses 8 Input Global Variables:
  15.                                                                                         LineSpriteID -- the id of the line (which is a sprite)
  16.                                                                                                 LineThickness -- The thickness of the line. Positive Float.
  17.                                                                                                 LineImageIndex -- The image (color) of the line.
  18.                                                                                                 DoThicknessOffset -- Set to true if you want the line to
  19.                                                         slightly extend beyond the start and 
  20.                                                          end points. Most drawing programs
  21.                                                         have this attribute set to true.
  22.                                                                                         LineStartx -- The x position of the start of the line.
  23.                                                                                         LineStarty -- The y position of the start of the line.
  24.                                                                                         LineEndx -- The x position of the end of the line.
  25.                                                                                         LineEndy -- The y position of the end of the line.
  26.  
  27. DrawLinePolar  :        This event uses 8 Input Global Variables:
  28.                                                                                         LineSpriteID -- the id of the line (which is a sprite)
  29.                                                                                                 LineThickness -- The thickness of the line. Positive Float.
  30.                                                                                                 LineImageIndex -- The image (color) of the line.
  31.                                                                                                 DoThicknessOffset -- Set to true if you want the line to
  32.                                                         slightly extend beyond the start and 
  33.                                                          end points. Most drawing programs
  34.                                                         have this attribute set to true.
  35.                                                                                         LineStartx -- The x position of the start of the line.
  36.                                                                                         LineStarty -- The y position of the start of the line.
  37.                                                                                         LineAngle -- The angle of the line.
  38.                                                                                         LineLength -- The length of the line.
  39.  
  40.  
  41. Example:  Make spriteofid(2) into a line of thickness 3. 
  42. Draw the line from (7,5) to (25,10).
  43.  
  44. GlobalVars  LineThickness LineSpriteID LineImageIndex DoThicknessOffset lineStartx lineStarty LineEndx LineEndy 
  45.  
  46. LineSpriteID = 2
  47. LineThickness = 3
  48. LineImageIndex = 5 //set the image to index 5, some colored square image.
  49. DoThicknessOffset = TRUE //This adds a slight lip to the ends of the line. It helps in
  50. // appending multiple lines. TRUE in most drawing programs.
  51. lineStartx = 7
  52. lineStarty = 5
  53. lineEndx = 25
  54. lineEndy = 10
  55.  
  56. SpriteOfID(1).ExecuteEvent($DrawLine) //Assuming this behavior is in spriteofid(1).
  57.  
  58.  
  59. Revision History:
  60.  
  61.  
  62. [Parameters]
  63.  
  64.  
  65. [200050 MP_DoLineTrig]
  66. GlobalVars LineSpriteID lineAngle linelength lineStartx lineStarty LineEndx LineEndy MP_LineT MP_LINEp45  MP_inverted MP_Linediffx MP_Linediffy MP_lineAnglesign MP_lineAnglepolarity 
  67. //This function does the ATan and SquareRoot calculations in order to determine the length and angle
  68. //of the line between the LineStart and LineEnd. They are the same functions found in the TrigMath behavior.
  69.  
  70. //First find the x and y distance between the start and the end of the line
  71. MP_Linediffx = (lineStartx-LineEndx)
  72. MP_Linediffy = (lineStarty-LineEndy)
  73.  
  74. MP_LineT = ABS(MP_Linediffx)/ ABS(MP_Linediffy) // The tangent ratio for the line.
  75.  
  76. //Now, correct for errors in the calculation of ArcTan evident when the angle is around 45 degrees.
  77. MP_LINEp45  = 0 //45 degree correction factor gets initialized.
  78. IF ( MP_LineT > 0.65 AND MP_LineT < 1.6) //If the ratio is in the "bad" zone, then let's get it out of there.
  79. //by rotating the line, then doing the calculation, and then rotating it back.
  80.     //First we will set up the dimensions of the line:
  81.     spriteofid(LineSpriteID).Stretch(lineStartx,lineStarty,LineEndx,lineStarty,LineEndx,LineEndy,lineStartx,LineEndy)  
  82.     spriteofid(LineSpriteID).Rotate(45) //rotate to get it out of the bad zone. and now, calculate the new difference:
  83.     MP_Linediffx = spriteofid(LineSpriteID).FirstCornerX - spriteofid(LineSpriteID).ThirdCornerX
  84.     MP_Linediffy = spriteofid(LineSpriteID).FirstCornerY - spriteofid(LineSpriteID).ThirdCornerY
  85.     //Rotate(-45) //We could rotate it back, but there is no need to do this since we'll be stretching it into a line.
  86.     MP_LINEp45  = -45
  87. ENDIF
  88.  
  89. //The ATan function we are using, only takes input from the first quadrant. But since
  90. //we want to determine the full angle, we will need to reflect the result to get
  91. //into the other quadrants. There are four possibilities:
  92. IF (ABS(MP_Linediffx) < ABS(MP_Linediffy))
  93.     MP_inverted = 90
  94.     MP_lineAnglesign = -1    
  95.     MP_LineT = MP_Linediffx/MP_Linediffy
  96.     IF (MP_Linediffy>1) 
  97.         MP_lineAnglepolarity = 0
  98.     ELSE
  99.         MP_lineAnglepolarity = 180
  100.     ENDIF        
  101. ELSE
  102.     MP_lineAnglesign = 1    
  103.     MP_inverted = 0
  104.     MP_LineT = MP_Linediffy/MP_Linediffx
  105.     IF (MP_Linediffx<1) 
  106.         MP_lineAnglepolarity = 0
  107.     ELSE
  108.         MP_lineAnglepolarity = 180
  109.     ENDIF           
  110. ENDIF    
  111. //Now, We calculate the first quadrant aTan.
  112. lineAngle=MP_LineT*(1+MP_LineT*MP_LineT*(-1/3+MP_LineT*MP_LineT*(1/5+MP_LineT*MP_LineT*(-1/7+MP_LineT*MP_LineT*(1/9-MP_LineT*MP_LineT/11)))))
  113. //Then we reflect and invert if needed to get into the other quadrants. Plus we add our 45degree correction factor (LINE45)
  114. lineAngle = MP_lineAnglesign*(lineAngle*180/PI + MP_inverted) - MP_lineAnglepolarity + MP_LINEp45 
  115.  
  116. //So far, we have calculated our angle. All that is left is to calculate the length of the line.
  117. //To do that we need a sqrt function, but we can be cleaver, and use the angle we just determined
  118. //to calculate the sqrt for us:
  119. IF (lineAngle = -180)
  120.     linelength = lineStartx - lineEndx
  121. ELSE    
  122.     spriteofid(LineSpriteID).Stretch(lineStartx,lineStarty,LineEndx,lineStarty,LineEndx,LineEndy,lineStartx,LineEndy)    
  123.     spriteofid(LineSpriteID).Rotate(-lineAngle)
  124.     linelength = spriteofid(LineSpriteID).BoundsRight - spriteofid(LineSpriteID).BoundsLeft       
  125. ENDIF    
  126. //That's it. We have all we need to draw our line: The angle to rotate, and the length.
  127. //The End. By Matthew.
  128.  
  129. [200051 DrawLine]
  130. //Simply execute DoLineTrig, then execute DrawLinePolar.
  131. ExecuteEvent(200050) 
  132. ExecuteEvent(200052)
  133.  
  134.  
  135. [200052 DrawLinePolar]
  136. GlobalVars  LineThickness LineSpriteID LineImageIndex DoThicknessOffset lineAngle linelength lineStartx lineStarty LineEndx LineEndy 
  137. GlobalVars MP_Linetempx MP_Linetempy MP_tempLineStartx MP_tempLineStarty MP_tempLineOffx MP_tempLineOffy
  138.  
  139.  
  140. //One usually executes event $DoLineTrig before $Draw line because we often draw lines between two  points.
  141. //$DoLineTrig Returns values in Dir2point and Dist2point. If these values are already known, then it isn't necessary
  142. //to first execute $DoLineTrig.
  143.  
  144. spriteofid(LineSpriteID).SetImageIndexTo(LineImageIndex) // Essentially sets the color of the Line
  145. //Add the length and width of the line to the starting position.
  146. MP_Linetempx = lineStartx + linelength //the length of the line 
  147. if(DoThicknessOffset and LineThickness > 1)         
  148. MP_Linetempx = MP_Linetempx + (LineThickness*DoThicknessOffset)  // added thickness if DoThicknessOffset
  149. endif
  150. MP_Linetempy = lineStarty + LineThickness //the width of the line
  151.  
  152. //Do the line manipulation.
  153. spriteofid(LineSpriteID).Stretch(lineStartx,lineStarty,MP_Linetempx,lineStarty,MP_Linetempx,MP_Linetempy,lineStartx,MP_Linetempy) //stretch out the line dimensions
  154. spriteofid(LineSpriteID).Rotate(lineAngle) //rotate by the angle determined in the trig function.
  155. MP_tempLineStartx = lineStartx - (spriteofid(LineSpriteID).firstcornerx + spriteofid(LineSpriteID).fourthcornerx)/2 //X Center of the start of the line
  156. MP_tempLineStarty = lineStarty - (spriteofid(LineSpriteID).firstcornery + spriteofid(LineSpriteID).fourthcornery)/2 //Y Center of the start of the line
  157. //Now if DoThicknessOffset is true, we will shift the line in the length direction by 1/2 of the thickness. Drawing programs usually
  158. //do this so that the tips of lines join together nicely. Good drawing programs will also clip the protruding corners off, but this is not
  159. //worth the effort when working with spites.
  160. if(DoThicknessOffset  and LineThickness > 1) //should we offset the edge of the sprite?
  161.     MP_tempLineOffx =(LineThickness/2)* (spriteofid(LineSpriteID).firstcornerx - spriteofid(LineSpriteID).secondcornerx)/(linelength + LineThickness)
  162.     MP_tempLineOffy =(LineThickness/2)* (spriteofid(LineSpriteID).firstcornery - spriteofid(LineSpriteID).secondcornery)/(linelength + LineThickness)
  163. endif
  164. //Now, move the line to get it into the correct position.
  165. spriteofid(LineSpriteID).moveby(MP_tempLineStartx+ MP_tempLineOffx,MP_tempLineStarty + MP_tempLineOffy)
  166.  
  167.  
  168.